package com.groupbuying.storeinternal.service.impl;

import com.groupbuying.storeinternal.connector.AdminFeign;
import com.groupbuying.storeinternal.connector.FileFegin;
import com.groupbuying.storeinternal.connector.SysconfigFeign;
import com.groupbuying.storeinternal.constants.CommonConstants;
import com.groupbuying.storeinternal.constants.StoreOpexRuleRelPreSearchConstants;
import com.groupbuying.storeinternal.context.FilterContextHandler;
import com.groupbuying.storeinternal.controller.forManage.StoreMainInfoController;
import com.groupbuying.storeinternal.dao.StoreMainInfoDao;
import com.groupbuying.storeinternal.domain.StoreCheckoutCounterDO;
import com.groupbuying.storeinternal.domain.StoreMainInfoDO;
import com.groupbuying.storeinternal.domain.StoreOpexRuleRelDO;
import com.groupbuying.storeinternal.domain.StoreShopDO;
import com.groupbuying.storeinternal.dto.StorePhotoDTO;
import com.groupbuying.storeinternal.dto.forExternal.StoreShopTempDTO;
import com.groupbuying.storeinternal.dto.forExternal.do2dto.StoreShopDetailConvert;
import com.groupbuying.storeinternal.pojo.StoreMainInfoEnum;
import com.groupbuying.storeinternal.service.StoreCheckoutCounterService;
import com.groupbuying.storeinternal.service.StoreMainInfoService;
import com.groupbuying.storeinternal.service.StoreOpexRuleRelService;
import com.groupbuying.storeinternal.service.StoreShopService;
import com.groupbuying.storeinternal.utils.PageUtils;
import com.groupbuying.storeinternal.utils.Query;
import com.groupbuying.storeinternal.utils.R;
import com.groupbuying.storeinternal.utils.apiresult.ApiResult;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class StoreMainInfoServiceImpl implements StoreMainInfoService {

	@Autowired
	private StoreMainInfoDao storeMainInfoDao;

	@Autowired
	private StoreShopService storeShopService;
	@Autowired
	private FileFegin fileFegin;
	@Autowired
	private SysconfigFeign sysconfigFeign;
	@Autowired
	private StoreOpexRuleRelService storeOpexRuleRelService;
	@Autowired
	private StoreCheckoutCounterService storeCheckoutCounterService;

	@Override
	public StoreMainInfoDO get(String uuid){
		return storeMainInfoDao.get(uuid);
	}
	
	@Override
	public List<StoreMainInfoDO> list(Map<String, Object> map){
		return storeMainInfoDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return storeMainInfoDao.count(map);
	}
	
	@Override
	public int save(StoreMainInfoDO storeMainInfo){
		return storeMainInfoDao.save(storeMainInfo);
	}
	
	@Override
	public int update(StoreMainInfoDO storeMainInfo){
		return storeMainInfoDao.update(storeMainInfo);
	}
	
	@Override
	public int remove(String uuid){
		return storeMainInfoDao.remove(uuid);
	}
	
	@Override
	public int batchRemove(String[] uuids){
		return storeMainInfoDao.batchRemove(uuids);
	}

	@Override
	public int updateByChainStoresUuid(Map<String, Object> map) {
		return storeMainInfoDao.updateByChainStoresUuid(map);
	}

    @Override
    public R getCrossUShopList(Map<String, Object> params) {

		try {
			params.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
			// 服务商id
			params.put("chainstoresuuid", FilterContextHandler.getName());
			params.put("auditstatus", StoreMainInfoEnum.BUSINESS_AUDIT_PASS);
			params.put("freezestatus", StoreMainInfoEnum.UNFROZEN);
			Query query = new Query(params);
			List<StoreMainInfoDO> list = storeMainInfoDao.getCrossUShopList(query);
			int total = storeMainInfoDao.getCrossUShopListCount(query);
			PageUtils pageUtil = new PageUtils(list, total);
			return R.ok().put("page", pageUtil);
		} catch (Exception e) {
			return R.error();
		}
    }

	@Override
	public List<Map<String, String>> getUuidAndStoreName() {
		return storeMainInfoDao.getUuidAndStoreName();
	}

	@Override
	public R getShopDetail(Map<String, Object> params) {

		Object temp = params.get("storeUuid");
		String storeUuid = temp != null ? temp.toString() : null;
		if (StringUtils.isBlank(storeUuid)) {
			return R.error("params is null");
		}
		StoreMainInfoDO storeMainInfoDO = storeMainInfoDao.get(storeUuid);
		StoreShopDO storeShopDO = storeShopService.getByStoreUuid(storeUuid);

		if (storeShopDO == null) {
			R.error();
		}

		Map queryCounterParam = new HashMap();
		queryCounterParam.put("delflag", CommonConstants.COMMON_FLAG_UNDEL);
		queryCounterParam.put("storeuuid", storeUuid);
		List<StoreCheckoutCounterDO> checkoutCounterDOList = storeCheckoutCounterService.list(queryCounterParam);
		String counterId = checkoutCounterDOList != null && checkoutCounterDOList.size() > 0 ? checkoutCounterDOList.get(0).getUuid(): null;

		StorePhotoDTO storePhotoDTO = new StorePhotoDTO();
		storePhotoDTO.setWithinPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getWithinphoto()));
		storePhotoDTO.setDoorPhoto(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getDoorphoto()));
		storePhotoDTO.setSlideshow(fileFegin.getDownloadUrlByFileGroupKey(storeShopDO.getSlideshow()));

		Map<String, Object> result = new HashMap<>();
		StoreShopTempDTO storeShopTempDTO = new StoreShopTempDTO();
		storeShopTempDTO.setStoreMainInfoDO(storeMainInfoDO);
		storeShopTempDTO.setStoreShopDO(storeShopDO);
		result.put("storePhotoDTO", storePhotoDTO);
		result.put("shopDetail", StoreShopDetailConvert.MAPPER.dTOToDTO(storeShopTempDTO));
		result.put("counterId",counterId);

		Map searchMap = new HashMap();
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_STOREID,storeUuid);
		searchMap.put(StoreOpexRuleRelPreSearchConstants.SEARCH_DELFLAG,CommonConstants.COMMON_FLAG_UNDEL);
		List <StoreOpexRuleRelDO> listOpex = storeOpexRuleRelService.list(searchMap);



		if (listOpex == null || listOpex.size() == 0) {
			return R.ok(result);
		}

		StoreOpexRuleRelDO storeOpexRuleRelDO = listOpex.get(0);
		Map ruleMap = new HashMap();
		ruleMap.put("ruleId", storeOpexRuleRelDO.getRuleId());
		ruleMap.put("delFlag", CommonConstants.COMMON_FLAG_UNDEL);
		Map ruleRes = sysconfigFeign.getStoreOpexRule(ruleMap);
		if (ruleRes != null) {
			BigDecimal backCurrency = new BigDecimal(ruleRes.get("backCurrencyRatio").toString());
			BigDecimal discount = new BigDecimal(ruleRes.get("currencyPayDiscount").toString());
			discount = discount.divide(new BigDecimal(10));
			result.put("ratio", backCurrency);
			result.put("discount", discount);
			result.put("ruleId", storeOpexRuleRelDO.getRuleId());
			result.put("ruldeDes", storeOpexRuleRelDO.getRuleDes());
		}
		return R.ok(result);
	}

    @Override
    public ApiResult getPaymentDayByCounterId(String counterId) {

		if(StringUtils.isBlank(counterId)){
			return ApiResult.failure("收银台id为空");
		}

		Map counterQueryParam = new HashMap();
		counterQueryParam.put("uuid",counterId);
		counterQueryParam.put("delflag",CommonConstants.COMMON_FLAG_UNDEL);
		List<StoreCheckoutCounterDO> counterList = storeCheckoutCounterService.list(counterQueryParam);
		StoreCheckoutCounterDO storeCheckoutCounterDO = counterList.size()>0 ? counterList.get(0): null;
		if(storeCheckoutCounterDO == null){
			return ApiResult.failure("收银台为空");
		}
		Map storeQueryParam = new HashMap();
		storeQueryParam.put("uuid",storeCheckoutCounterDO.getStoreuuid());
		storeQueryParam.put("delflag",CommonConstants.COMMON_FLAG_UNDEL);
		List<StoreMainInfoDO> storeList = storeMainInfoDao.list(storeQueryParam);
		StoreMainInfoDO storeMainInfoDO = storeList.size()>0 ? storeList.get(0): null;
		if(storeMainInfoDO == null){
			return ApiResult.failure("收银台所属商户为空");
		}

		return ApiResult.success(storeMainInfoDO.getDetectionrandomtime());
    }


}
